package com.guangzhi.weijianzhi.mainhome;


import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.guangzhi.weijianzhi.R;
import com.guangzhi.weijianzhi.activity.BaseActivity;
import com.guangzhi.weijianzhi.activity.MainPageActivity;
import com.guangzhi.weijianzhi.adapter.SimpleBaseAdapter;
import com.guangzhi.weijianzhi.utils.Misc;
import com.guangzhi.weijianzhi.view.MyLetterListView;
import com.guangzhi.weijianzhi.view.MyLetterListView.OnTouchingLetterChangedListener;
/**
 * 位置检索
 * @author Administrator
 *
 */
public class CitysSearchActivity extends BaseActivity implements OnClickListener, OnItemClickListener {
	private LocationClient mLocationClient = null;
	private BDLocationListener myListener = new MyLocationListener();
	private ArrayList<CitysSearchEntity>entitya = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityb = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityc = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityd = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entitye = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityf = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityg = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityh = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityi = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityj = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityk = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityl = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entitym = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityn = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityo = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityp = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityq = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityr = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entitys = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityt = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityu = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityv = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityw = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityx = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityy = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityz = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private ArrayList<CitysSearchEntity>entityss = new ArrayList<CitysSearchActivity.CitysSearchEntity>();
	private CitysSearchEntity entity;
	private Map<String, Integer> map = new HashMap<String, Integer>();
	private ListView mListView;
	private MyHandle handle;
	private TextView overlay;
	private Myadapter mAdapter;
	private RadioButton mCitys;
	class MyHandle extends Handler{
		  @Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1000:
				overlay.setVisibility(View.GONE);
				break;
			}
		}
		  
	 } 
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.citys_search_layout);
		initView();
		initData();
		//把数据放到集合里
		map.put("A", 1);
		entityss.addAll(entitya);map.put("B", entityss.size()+1);entityss.addAll(entityb);map.put("C", entityss.size()+1);entityss.addAll(entityc);map.put("D", entityss.size()+1);
		entityss.addAll(entityd);map.put("E", entityss.size()+1);entityss.addAll(entitye);map.put("F", entityss.size()+1);
		entityss.addAll(entityf);map.put("G", entityss.size()+1);entityss.addAll(entityg);map.put("H", entityss.size()+1);entityss.addAll(entityh);map.put("I", entityss.size()+1);
		entityss.addAll(entityi);map.put("J", entityss.size()+1);entityss.addAll(entityj);map.put("K", entityss.size()+1);
		entityss.addAll(entityk);map.put("L", entityss.size()+1);entityss.addAll(entityl);map.put("M", entityss.size()+1);entityss.addAll(entitym);map.put("N", entityss.size()+1);
		entityss.addAll(entityn);map.put("O", entityss.size()+1);entityss.addAll(entityo);map.put("P", entityss.size()+1);
		entityss.addAll(entityp);map.put("Q", entityss.size()+1);entityss.addAll(entityq);map.put("R", entityss.size()+1);entityss.addAll(entityr);map.put("S", entityss.size());
		entityss.addAll(entitys);map.put("T", entityss.size()+1);entityss.addAll(entityt);map.put("U", entityss.size()+1);
		entityss.addAll(entityu);map.put("V", entityss.size()+1);entityss.addAll(entityv);map.put("W", entityss.size()+1);entityss.addAll(entityw);map.put("X", entityss.size()+1);
		entityss.addAll(entityx);map.put("Y", entityss.size()+1);entityss.addAll(entityy);map.put("Z", entityss.size()+1);
		entityss.addAll(entityz);
		mAdapter.onBoundData(entityss);
	}


	private void initView() {
		// TODO Auto-generated method stub
		initTitle();
		goBack.setOnClickListener(this);
		titleTextV.setText("位置检索");
		mListView = (ListView) findViewById(R.id.city_list);
		
		mAdapter = new Myadapter(this);
		addHead(mListView);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(this);
		handle = new MyHandle();
		MyLetterListView LetterListView = (MyLetterListView) findViewById(R.id.cityLetterListView);
		overlay = (TextView) findViewById(R.id.overlay);
		LetterListView.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {
			@Override
			public void onTouchingLetterChanged(String s) {
				overlay.setVisibility(View.VISIBLE);
				overlay.setText(s);
				if(map.size()!=0){
					if(map.get(s)!=null){
					int  curr = map.get(s);
					mListView.setSelection(curr);
					}
					Message mag = new Message();
					
					mag.what = 1000;
				   handle.sendMessageDelayed(mag, 2000);
				}
				
			}
		});
		mLocationClient = new LocationClient(this); // 声明LocationClient类
		mLocationClient.registerLocationListener(myListener); // 注册监听函数
		setLocationOption();
		mLocationClient.start();// 开始定位

		if (mLocationClient != null && mLocationClient.isStarted())
			mLocationClient.requestLocation();	
	}

	private void addHead(ListView mListView2) {
		// TODO Auto-generated method stub
		
		View inflate = View.inflate(this, R.layout.tz_city_head_layout, null);
		inflate.findViewById(R.id.city_beijing).setOnClickListener(this);
		inflate.findViewById(R.id.city_shanghai).setOnClickListener(this);
		inflate.findViewById(R.id.city_guangzhou).setOnClickListener(this);
		inflate.findViewById(R.id.city_shenzhen).setOnClickListener(this);
		inflate.findViewById(R.id.city_hangzhou).setOnClickListener(this);
		inflate.findViewById(R.id.city_nanjing).setOnClickListener(this);
		inflate.findViewById(R.id.city_tianjin).setOnClickListener(this);
		inflate.findViewById(R.id.city_wuhan).setOnClickListener(this);
		inflate.findViewById(R.id.city_chongqing).setOnClickListener(this);
		mCitys = (RadioButton) inflate.findViewById(R.id.posi_city);
		mListView2.addHeaderView(inflate);
	}


	private void initData() {
		// TODO Auto-generated method stub
		InputStream inputStream = getResources().openRawResource(
				R.raw.locationare);
		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = factory.newPullParser();
			parser.setInput(inputStream, "utf-8");
			int eventType = parser.getEventType();

			while (eventType != XmlPullParser.END_DOCUMENT) {
				String tag = parser.getName();
				switch (eventType) {
				case XmlPullParser.START_TAG:
					if ("c".equals(tag)) {
						entity = new CitysSearchEntity();
					}
					if("id".equals(tag)){
						entity.pCode = parser.nextText();
					}
					if("provinceId".equals(tag)){
						entity.cCode = parser.nextText();
					}
					if("engf".equals(tag)){
						entity.engf = parser.nextText();
					}
					if("name".equals(tag)){
						entity.name = parser.nextText();
					}
					
					break;

				case XmlPullParser.END_TAG:

					if ("c".equals(tag)) {
						String engf = entity.engf;
						
						if("A".equals(engf)){
							entitya.add(entity);
						}else if("B".equals(engf)){
							entityb.add(entity);
						}else if("C".equals(engf)){
							entityc.add(entity);
						}else if("D".equals(engf)){
							entityd.add(entity);
						}else if("E".equals(engf)){
							entitye.add(entity);
						}else if("F".equals(engf)){
							entityf.add(entity);
						}else if("G".equals(engf)){
							entityg.add(entity);
						}else if("H".equals(engf)){
							entityh.add(entity);
						}else if("I".equals(engf)){
							entityi.add(entity);
						}else if("J".equals(engf)){
							entityj.add(entity);
						}else if("K".equals(engf)){
							entityk.add(entity);
						}else if("L".equals(engf)){
							entityl.add(entity);
						}else if("M".equals(engf)){
							entitym.add(entity);
						}else if("N".equals(engf)){
							entityn.add(entity);
						}else if("O".equals(engf)){
							entityo.add(entity);
						}else if("P".equals(engf)){
							entityp.add(entity);
						}else if("Q".equals(engf)){
							entityq.add(entity);
						}else if("R".equals(engf)){
							entityr.add(entity);
						}else if("S".equals(engf)){
							entitys.add(entity);
						}else if("T".equals(engf)){
							entityt.add(entity);
						}else if("U".equals(engf)){
							entityu.add(entity);
						}else if("V".equals(engf)){
							entityv.add(entity);
						}else if("W".equals(engf)){
							entityw.add(entity);
						}else if("X".equals(engf)){
							entityx.add(entity);
						}else if("Y".equals(engf)){
							entityy.add(entity);
						}else if("Z".equals(engf)){
							entityz.add(entity);
						}
						entity =null;
					}
					break;
				}
				eventType = parser.next();
			}
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private class CitysSearchEntity{
		
		public String pCode;
		
		public String name;
		
		public String engf;
		
		public String cCode;
		
	}
	
	private class Myadapter extends SimpleBaseAdapter<CitysSearchEntity>{

		public Myadapter(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
		}

		@Override
		public int getItemResource() {
			// TODO Auto-generated method stub
			return R.layout.tz_citys_search_item_layout;
		}

		@Override
		public View getItemView(
				int position,
				View convertView,
				com.guangzhi.weijianzhi.adapter.SimpleBaseAdapter.ViewHolder holder) {
			// TODO Auto-generated method stub
			TextView alpha = (TextView) holder.getView(R.id.alpha);
			TextView name = (TextView) holder.getView(R.id.name);
			
			String str = position==0?"":getDatas().get(position-1).engf;
			if(str.equals(getDatas().get(position).engf)){//如果前一个字母跟当前相同
				if(position==0){
					alpha.setVisibility(View.VISIBLE);
					alpha.setText(getDatas().get(position).engf);
				}else{
				alpha.setVisibility(View.GONE);
				}
			}else{
				alpha.setVisibility(View.VISIBLE);
				alpha.setText(getDatas().get(position).engf);
			}
			
			name.setText(getDatas().get(position).name);
			return convertView;
		}}


	@Override
	public void onClick(View v) {
		Intent  intent = new Intent(this, MainPageActivity.class);
		Bundle bundle2 = new Bundle();
		switch (v.getId()) {
		
		case R.id.city_beijing:
			bundle2.putString("name", "北京");
			bundle2.putString("code", "100000");
			
			break;
		case R.id.city_shanghai:
			bundle2.putString("name", "上海");
			bundle2.putString("code", "110000");
			break;
		case R.id.city_guangzhou:
			bundle2.putString("name", "广州");
			bundle2.putString("code", "151800");
			break;
		case R.id.city_shenzhen:
			bundle2.putString("name", "深圳");
			bundle2.putString("code", "151900");
			break;
		case R.id.city_hangzhou:
			bundle2.putString("name", "杭州");
			bundle2.putString("code", "141500");
			break;
		case R.id.city_nanjing:
			bundle2.putString("name", "南京");
			bundle2.putString("code", "161600");
			break;
		case R.id.city_tianjin:
			bundle2.putString("name", "天津");
			bundle2.putString("code", "120000");
			break;
		case R.id.city_wuhan:
			bundle2.putString("name", "武汉");
			bundle2.putString("code", "212100");
			break;
		case R.id.city_chongqing:
			bundle2.putString("name", "重庆");
			bundle2.putString("code", "130000");
			break;
		
	    case R.id.title_back:
		onBackPressed();
		  break;
		}
		
		intent.putExtra("bund", bundle2);
		setResult(200, intent);
		finish();
	}


	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		String name = entityss.get(arg2-1).name;
		String pCode = entityss.get(arg2-1).pCode;
		Intent intent = new Intent(this, MainPageActivity.class);
		Bundle bundle2 = new Bundle();
		bundle2.putString("name", name);
		bundle2.putString("code", pCode);
		intent.putExtra("bund", bundle2);
		setResult(200, intent);
		finish();
		
	}
	
	/**
	 * 设置相关参数
	 */
	private void setLocationOption() {
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);
		option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
		option.setAddrType("all");// 返回的定位结果包含地址信息
		option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
		option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
		option.disableCache(true);// 禁止启用缓存定位
		option.setPoiNumber(5); // 最多返回POI个数
		option.setPoiDistance(1000); // poi查询距离
		option.setPoiExtraInfo(true); // 是否需要POI的电话和地址等详细信息
		mLocationClient.setLocOption(option);
	}
	
	
	/**
	 * @author guangzhi 获取经纬度
	 */
	public class MyLocationListener implements BDLocationListener {
		

		@Override
		public void onReceiveLocation(BDLocation location) {
			Misc.log(location + "------location--->");
			if (location == null) {
				return;
			}

			String city = location.getCity();
			mCitys.setText(city);
			String Area = location.getAddrStr();

			String cityCode = location.getCityCode();
			
			String district = location.getDistrict();
			mLocationClient.stop();

		}

		@Override
		public void onReceivePoi(BDLocation arg0) {
			// TODO Auto-generated method stub

		}
	}
}
